package org.xw.service.impl;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xw.dto.GzhDTO;
import org.xw.mapper.GzhArticleMapper;
import org.xw.mapper.GzhMapper;
import org.xw.mapper.MyGzhMapper;
import org.xw.pojo.GzhArticlePojo;
import org.xw.pojo.GzhPojo;
import org.xw.pojo.MyGzhPojo;
import org.xw.service.GzhService;
import org.xw.utils.JWTUtil;
import org.xw.utils.ResponseUtil;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class GzhServiceImpl implements GzhService {
    @Autowired
    private GzhMapper gzhMapper;
    @Autowired
    private GzhArticleMapper gzhArticleMapper;
    @Autowired
    private MyGzhMapper myGzhMapper;

    /**
     * 注册公众号
     *
     * @param
     */
    @Override
    public ResponseUtil registerOfficialAccount(GzhPojo gzhPojo) {
        if (gzhMapper.selectByName(gzhPojo.getGzhName()) == null) {
            int insert = gzhMapper.insert(gzhPojo);
            if (insert != 0) {
                return ResponseUtil.success("注册成功");
            } else return ResponseUtil.error("服务器出了点错误");
        } else {
            return ResponseUtil.error("用户名重复，请重试！");
        }
    }

    /**
     * 注销公众号
     *
     * @param gzhId
     */
    @Override
    public boolean deleteOfficialAccount(Integer gzhId) {
        return gzhMapper.deleteGzh(gzhId);
    }

    /**
     * 修改公众号信息
     *
     * @param
     * @return
     */
    public int updateOfficialAccount(GzhPojo gzhPojo) {
        return gzhMapper.updateGzh(gzhPojo);
    }

    /**
     * 搜索公众号
     *
     * @param gzhName
     * @return
     */
    @Override
    public ResponseUtil searchOfficialAccount(String gzhName) {
        List<GzhPojo> gzhPojo = gzhMapper.searchOfficialAccount(gzhName);
        return ResponseUtil.success(gzhPojo);
    }

    /**
     * 管理公众号
     *
     * @param gzhId
     */
    @Override
    public ResponseUtil followOfficialAccount(HttpServletRequest httpServletRequest, Integer gzhId) {
        String userId = JWTUtil.analyzeToken(httpServletRequest, "Authorization", "user_id");
        int i = gzhMapper.isFollow(Integer.valueOf(userId), gzhId);
        if (i == 0) {
            myGzhMapper.insert(Integer.valueOf(userId), gzhId);//关注
        } else {
            myGzhMapper.delete(Integer.valueOf(userId), gzhId);//取关
        }
        return ResponseUtil.success(null);
    }


    /**
     * 查看已关注公众号列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<GzhDTO> getOfficialAccountList(Integer userId) {
        List<GzhDTO> myGzhList = myGzhMapper.getMyGzhList(userId);
        for (GzhDTO myGzhPojo : myGzhList) {
            GzhPojo gzhPojo = myGzhMapper.selectInfo(myGzhPojo.getGzhId());
            myGzhPojo.setGzhInfo(gzhPojo);
        }
        return myGzhList;

    }

    /**
     * 公众号详情
     *
     * @param id
     * @return
     */
    @Override
    public GzhPojo getOfficialAccount(Integer id) {
        return gzhMapper.getOfficialAccount(id);
    }

    /**
     * 公众号文章列表
     *
     * @param gzhId
     * @return
     */
    @Override
    public List<GzhArticlePojo> getGzhArticleList(HttpServletRequest httpServletRequest, Integer gzhId) {
        return gzhArticleMapper.getGzhArticleList(gzhId);//文章status=1(审核通过)
    }

    /**
     * 公众号文章
     *
     * @param id
     * @return
     */
    @Override
    public GzhArticlePojo getOfficialAccountArticles(Integer id) {
        return gzhArticleMapper.searchArticle(id);
    }

    /**
     * 发布文章
     *
     * @param
     */
    @Override
    public ResponseUtil publishArticle(GzhArticlePojo gzhArticlePojo) {
        if (gzhArticlePojo.getGzhId() != null) {
            LocalDateTime now = LocalDateTime.now();
            gzhArticlePojo.setCreateTime(now);
            gzhArticlePojo.setUpdateTime(now);
            int i = gzhArticleMapper.publishArticle(gzhArticlePojo);
            if (i != 0) {
                return ResponseUtil.success("发布成功");
            } else return ResponseUtil.error("文章发布错误，请重试！");

        } else {
            return ResponseUtil.error("文章发布错误，请重试！");
        }
    }

    /**
     * 删除文章
     */
    @Override
    public boolean deleteArticle(GzhArticlePojo articlePojo) {
        return gzhArticleMapper.deleteArticle(articlePojo);
    }

    /**
     * 编辑文章
     *
     * @return
     */
    @Override
    public ResponseUtil updateArticle(GzhArticlePojo gzhArticlePojo) {
        LocalDateTime now = LocalDateTime.now();
        gzhArticlePojo.setUpdateTime(now);
        boolean b = gzhArticleMapper.updateArticle(gzhArticlePojo);
        if (b) {
            return ResponseUtil.success("更新成功");
        } else return ResponseUtil.error("更新失败");
    }

}

